<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f0f0f0;
            margin: 0;
            padding: 0;
            display: flex;
            flex-direction: column;
            min-height: 100vh;
            align-items: center;
            justify-content: center;
        }
        .image-list {
            background-color: #fff;
            border-radius: 10px;
            padding: 2rem;
            box-shadow: 0px 2px 6px rgba(0, 0, 0, 0.1);
            max-width: 800px;
            width: 100%;
            text-align: center;
        }
        .image-list h1 {
            margin-bottom: 1rem;
            color: #007BFF;
        }
        .image-list a {
            color: #007BFF;
            text-decoration: none;
            transition: color 0.3s;
        }
        .image-list a:hover {
            color: #0056b3;
        }
        .image-preview {
            margin: 2rem 0;
        }
        .image-preview img {
            width: 100%;
            max-width: 500px;
            height: auto;
            margin-bottom: 0.5rem;
            border: 1px solid #ccc;
            border-radius: 5px;
        }
        .image-info {
            font-size: 14px;
            color: #555;
        }
        .pagination {
            margin-top: 1rem;
            display: flex;
            justify-content: center;
        }
        .pagination a {
            display: inline-block;
            padding: 0.3rem 0.5rem;
            margin: 0 0.2rem;
            background-color: #007BFF;
            color: #fff;
            border-radius: 5px;
            text-decoration: none;
        }
        .pagination a:hover {
            background-color: #0056b3;
        }
        .image-list h1 {
            margin-bottom: 1rem;
            color: #007BFF;
        }
        .image-list h1 span {
            color: #000;
        }
        /* 페이지네이션 스타일 */
        .pagination {
            margin-top: 1rem;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .pagination-link {
            display: inline-block;
            padding: 0.3rem 0.5rem;
            margin: 0 0.2rem;
            background-color: #007BFF;
            color: #fff;
            border-radius: 5px;
            text-decoration: none;
            transition: background-color 0.3s;
        }

        .pagination-link:hover {
            background-color: #0056b3;
        }

        .current-page {
            padding: 0.3rem 0.5rem;
            margin: 0 0.2rem;
            background-color: #eee;
            color: #333;
            border-radius: 5px;
        }
        .image-preview {
            margin: 2rem 0;
            border: 1px solid #ccc;
            padding: 1rem;
            border-radius: 5px;
            background-color: #fff;
        }
        .image-grid {
            display: flex;
            flex-wrap: wrap;
            justify-content: space-around;
        }
        .image-preview {
            flex-basis: calc(50% - 10px);
            box-sizing: border-box;
            margin: 5px;
        }
        body.dark-mode {
            background-color: #121212;
            color: #ffffff;
        }

        .dark-mode .image-list {
            background-color: #1e1e1e;
            color: #c0c0c0;
        }

        .dark-mode .image-info,
        .dark-mode .pagination-link,
        .dark-mode .current-page {
            color: #c0c0c0;
        }
        .dark-mode .highlight {
            color: #ffffff;
        }

        .dark-mode .image-info {
            color: #000000;
        }
    </style>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Patient Images</title>
</head>
<body>
<div class="image-list">
    <h1>{{ table_name }} <span class="highlight">Images for </span>{{ user_name }}</h1>
    <p><a href="{{ url_for('main_page') }}">Main Page</a> | <a href="{{ url_for('check_patients_page') }}">Previous
        Page</a></p>
    <button id="toggleDarkMode">Toggle Dark Mode</button>
    <p class="image-info">The probability value is rounded to the fourth decimal place</p>

    <!-- 이미지 출력 -->
    <div class="image-grid"> <!-- Added a container for the image grid -->
        {% for image_data, upload_time, probability in images %}
        <div class="image-preview">
            <img src="data:image/jpeg;base64,{{ image_data }}" alt="{{ upload_time }} Probability: {{ probability }}"/>
            <p class="image-info">Time: {{ upload_time }} | Probability: {{ '{:.3f}'.format(probability) }}</p>
        </div>
        {% if loop.index % 2 == 0 %} <!-- Add a line break after every 2 images -->
        <div class="break"></div>
        {% endif %}
        {% endfor %}
    </div>

    <!-- 페이지네이션 -->
    <div class="pagination">
        {% if current_page > 1 %}
        <a class="pagination-link"
           href="{{ url_for('check_patients_paged', user_name=user_name, table_name=table_name, page=current_page-1) }}">Previous</a>
        {% endif %}
        <span class="current-page">Page {{ current_page }}</span>
        <a class="pagination-link"
           href="{{ url_for('check_patients_paged', user_name=user_name, table_name=table_name, page=current_page+1) }}">Next</a>
    </div>
</div>

<script>
    // 이미지를 새 탭으로 열기
    function openImageInNewTab(imageSrc) {
        var newTab = window.open();
        newTab.document.write("<img src='" + imageSrc + "' alt='Image' style='max-width: 100%; max-height: 100%;'>");
        newTab.document.close();
    }

    var images = document.querySelectorAll(".image-preview img");

    images.forEach(function(image) {
        image.onclick = function() {
            openImageInNewTab(this.src);
        };
    });
    document.addEventListener('DOMContentLoaded', () => {
        const toggleButton = document.getElementById('toggleDarkMode');

        if (localStorage.getItem('dark-mode') === 'true') {
            document.body.classList.add("dark-mode");
        }

        toggleButton.addEventListener('click', () => {
            document.body.classList.toggle("dark-mode");
            localStorage.setItem('dark-mode', document.body.classList.contains("dark-mode"));
        });
    });
</script>
</body>
</html>
